import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from  '../components/user/User'
import Rights from  '../components/power/rights'
import Roles from  '../components/power/roles'
import Categories from  '../components/goods/Categories'
import Params from  '../components/goods/Params'
import Goods from  '../components/goods/goodsList'
import Add from  '../components/goods/add'
import Orders from  '../components/order/Order'
import Reports from  '../components/report/Report'

Vue.use(VueRouter)

const routes = [
    //重定向到登陆界面
    {path: '/', redirect: '/login'},

    {path: '/login', component: Login},
    {
        path: '/home', component: Home, children: [
            {path: '/', redirect: '/welcome'},
            {path: '/welcome', component: Welcome},
            {path: '/users', component: Users},
            {path: '/rights', component: Rights},
            {path: '/roles', component: Roles},
            {path: '/categories', component: Categories},
            {path: '/params', component: Params},
            {path: '/goods', component: Goods},
            {path: '/goods/add', component:Add},
            {path: '/orders', component:Orders},
            {path: '/reports', component:Reports},
        ]
    }

]

const router = new VueRouter({
    routes
})
//挂载路由导航守卫
router.beforeEach((to, form, next) => {
    //to 将要访问的路径   form 从哪个路径跳转而来   next是一个函数,表示放行
    // next('/login')强制跳转
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login') //没有token  强制跳转会登陆页
    next();
})


export default router
